//设置本地存储
export const setLocal = (key: string, value: any) => {
    if (key && value) {
        localStorage.setItem(key, JSON.stringify(value));
    } else {
        return null;
    }
};
//根据KEY值获取本地存储的值
export const getLocal = (key: string) => {
    const data = key ? localStorage.getItem(key) : "";
    return data ? JSON.parse(data) : null;
};
//删除本地存储指定KEY的值
export const removeLocal = (key: string) => {
    key ? localStorage.removeItem(key) : "";
};
//设置session存储
export const setSession = (key: string, value: any) => {
    if (key && value) sessionStorage.setItem(key, JSON.stringify(value));
};
//根据KEY值获取session存储的值
export const getSession = (key: string) => {
    const data = key ? sessionStorage.getItem(key) : "";
    return data ? JSON.parse(data) : "";
};
//删除session存储指定KEY的值
export const removeSession = (key: string) => {
    key ? sessionStorage.removeItem(key) : "";
};

// 判断类型集合
const checkStr = (str: any, type: any) => {
    switch (type) {
        case "mobile": //手机号码
            return /^1[0-9]{10}$/.test(str);
        case "tel": //座机
            return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(str);
        case "card": //身份证
            return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(str);
        case "mobileCode": //6位数字验证码
            return /^[0-9]{6}$/.test(str);
        case "pwd": //密码以字母开头，长度在6~18之间，只能包含字母、数字和下划线
            return /^([a-zA-Z0-9_]){6,18}$/.test(str);
        case "payPwd": //支付密码 6位纯数字
            return /^[0-9]{6}$/.test(str);
        case "postal": //邮政编码
            return /[1-9]\d{5}(?!\d)/.test(str);
        case "QQ": //QQ号
            return /^[1-9][0-9]{4,9}$/.test(str);
        case "email": //邮箱
            return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str);
        case "money": //金额(小数点2位)
            return /^\d*(?:\.\d{0,2})?$/.test(str);
        case "URL": //网址
            return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(
                str
            );
        case "IP": //IP
            return /((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/.test(
                str
            );
        case "date": //日期时间
            return (
                /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2})(?:\:\d{2}|:(\d{2}):(\d{2}))$/.test(
                    str
                ) || /^(\d{4})\-(\d{2})\-(\d{2})$/.test(str)
            );
        case "number": //数字
            return /^[0-9]$/.test(str);
        case "english": //英文
            return /^[a-zA-Z]+$/.test(str);
        case "chinese": //中文
            return /^[\\u4E00-\\u9FA5]+$/.test(str);
        case "lower": //小写
            return /^[a-z]+$/.test(str);
        case "upper": //大写
            return /^[A-Z]+$/.test(str);
        case "HTML": //HTML标记
            return /<("[^"]*"|'[^']*'|[^'">])*>/.test(str);
        default:
            return true;
    }
};

//获取URL各参数值
const getUrl = (flag: any) => {
    if (flag === 1) {
        return location.href.split("?")[1];
    } else {
        const obj: any = [];
        const hrefs = location.href.split("?");
        if (hrefs[1]) {
            const keyValue = hrefs[1].split("&");
            if (keyValue) {
                for (let i = 0; i < keyValue.length; i++) {
                    const item = keyValue[i].split("=");
                    obj[item[0]] = item[1];
                }
            }
        }
        return obj;
    }
};

//获取URL指定名称的值
const getUrlParam = (name: any) => {
    if (!name) return false;
    const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    const result = window.location.search.substr(1).match(reg);
    return result ? decodeURIComponent(result[2]) : null;
};

//过滤手机号或身份证号或姓名位数为*号
const judegString = (val: any, flag = null) => {
    if (!val || !flag) return "";
    if (flag == 1) {
        //手机号隐藏*号
        const reg = /^(.{3}).*(.{4})$/;
        return val.replace(reg, "$1****$2");
    } else if (flag == 2) {
        //身份证号隐藏*号
        const reg = /^(.{4}).*(.{4})$/;
        return val.replace(reg, "$1****$2");
    } else if (flag == 3) {
        //姓名隐藏*号
        let name = "";
        if (val.length === 2) {
            name = val.substring(0, 1) + "*"; // 截取name 字符串截取第一个字符，
        } else if (val.length === 3) {
            name = val.substring(0, 1) + "*" + val.substring(2, 3); // 截取第一个和第三个字符
        } else if (val.length > 3) {
            name =
                val.substring(0, 1) +
                "**" +
                val.substring(val.length - 1, val.length); // 大于3个字只显示头尾中间用*号
        }
        return name;
    }
};

// 截取当前数据到小数点后两位
const numFilter = (val: any, fix = 2) => {
    if (val !== 0) {
        val = Number(val); // 字符串转为数字，目标数据为数字可不转
        val = "" + val.toFixed(2); // 保留两位小数并转为字符串
        let int = val.slice(0, fix * -1 - 1); // 获取整数
        const ext = val.slice(fix * -1 - 1); // 获取到小数
        int = int.split("").reverse().join(""); // 翻转整数
        let temp = ""; // 临时变量
        for (let i = 0; i < int.length; i++) {
            temp += int[i];
            if ((i + 1) % 3 === 0 && i !== int.length - 1) {
                temp += ","; // 每隔3个数字拼接一个逗号
            }
        }
        temp = temp.split("").reverse().join(""); // 加完逗号之后翻转
        temp = temp + ext; // 整数小数拼接
        return temp; // 返回
    } else {
        return val;
    }
};

//过滤关键字返回*号
const maskSensitiveWords = (text: any, words: any) => {
    const regex = new RegExp(`(?:${words.join("|")})`, "gi");
    return text.replace(regex, "*".repeat(words.length));
};

export default {
    checkStr,
    getUrl,
    getUrlParam,
    judegString,
    numFilter,
    maskSensitiveWords,
};
